import json
import urllib
import urllib.request
from urllib import parse
from urllib.request import Request


# data = {"name":"wangxu","age":20}
# url_encode = parse.urlencode(data)
# print(url_encode)
# url = f'http://httpbin.org/get?{url_encode}'
# response = urllib.request.urlopen(url)
# # print(dir(response))
# if response.status == 200:
#     response_bytes = response.read()
#     response_str = response_bytes.decode('utf-8')
#     response_json = json.loads(response_str)
#     print(response_json)
#     print(type(response_json),response_json['args'],response_json['headers']["User-Agent"])




# url = 'http://httpbin.org/get?name=xuxu'
# req = Request(url,headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0"},)
# response = urllib.request.urlopen(req)
# # print(dir(response))
#
# if response.status == 200:
#     response_bytes = response.read()
#     response_str = response_bytes.decode('utf-8')
#     response_json = json.loads(response_str)
#     print(response_json)
#     print(response_json["headers"]["User-Agent"])


url = 'http://httpbin.org/post?name=xuxu'
data = {"age":20,"name":"wangxu"}
url_encode = parse.urlencode(data)
print(url_encode)
req = Request(url,data=url_encode.encode(),headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0",
                                                'content-type':'application/x-www-form-urlencoded'},method="POST")
response = urllib.request.urlopen(req)


if response.status == 200:
    response_bytes = response.read()
    response_str = response_bytes.decode('utf-8')
    response_json = json.loads(response_str)
    print(response_json)
    # print(response_json["headers"]["User-Agent"])
















